#!/usr/bin/env python
#coding=utf-8

import datetime
import urllib2
import urllib
import json
import sys

post_url="http://192.168.10.195:8000/api/url_count/"


import datetime

now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-1)
access_time = yes_time.strftime('%Y-%m-%d')

host=sys.argv[1]
port=sys.argv[2]

file = "/usr/local/{0}/logs/localhost_access_log.{1}.txt".format(port,access_time)



urls={}
f=open(file,'r')
lines=f.readlines()

for line in lines:
  line=line.replace('?',' ')
  line=line.replace(';',' ')
  line=line.split(' ')
  url=line[4]
  if urls.has_key(url):
    urls[url] += 1
  else:
    urls[url] = 1
urls = json.dumps(urls)

data={"host":host,
      "port":port, 
      "urls":urls,
      "access_time":access_time
}
data=json.dumps(data)


data = {"data":data}
post_data = urllib.urlencode(data)
req = urllib2.urlopen(post_url,post_data)
conntent = req.read()
print conntent

